package com.wxl.www.wxl.shiro.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxl.www.wxl.shiro.entity.UserInfo;
import com.wxl.www.wxl.shiro.service.UserInfoService;
import com.wxl.www.wxl.shiro.utils.CreateUUID;
import com.wxl.www.wxl.shiro.utils.DataUtil;
import com.wxl.www.wxl.shiro.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/sys/user")
public class SysUserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/list")
    public String list(
            @RequestParam(value="pageNo", defaultValue="1")Integer pageNo
            , @RequestParam(value="pageSize", defaultValue="10") Integer pageSize
            , Model model){
        IPage<UserInfo> page = new Page<>(pageNo, pageSize);
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("update_time");
        UserInfo userInfo = new UserInfo();
        wrapper.setEntity(userInfo);
        model.addAttribute("userinfoList",userInfoService.page(page,wrapper));
        return "sys/user/list";
    }

    /**
     * 添加跳转
     * @return
     */
    @RequestMapping("/inster")
    public String toInster(){
        return "sys/user/inster";
    }

    /**
     * 添加保存
     * @param userInfo
     * @return
     */
    @PostMapping("/inster")
    @ResponseBody
    public ResultUtil inster(UserInfo userInfo){
        try {
            userInfo.setId(CreateUUID.getUUID32());
            userInfo.setUpdateTime(DataUtil.getDate());
            boolean save = userInfoService.save(userInfo);
            System.out.println(userInfo.getPhone());
            return new ResultUtil();
        }catch (Exception e){
            System.out.println(e);
        }
        return null;
    }

    /**
     * 修改跳转
     * @return
     */
    @RequestMapping("/update")
    public String toUpdate(){
        return "sys/user/update";
    }

    /**
     * 修改保存
     * @return
     */
    @PostMapping("update")
    public ResultUtil update(){
        return new ResultUtil();
    }

    /**
     * 删除
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public ResultUtil delete(String id){
        ResultUtil resultUtil = new ResultUtil();
        if(userInfoService.removeById(id)){
            resultUtil.setCode(200);
        }
        return resultUtil;
    }

    /**
     * 查看详情
     * @return
     */
    @PostMapping("/view")
    public ResultUtil view(){
        return new ResultUtil();
    }
}
